package com.xunqi.permission.controller;

import com.google.common.collect.Maps;
import com.xunqi.permission.beans.PageQuery;
import com.xunqi.permission.common.JsonData;
import com.xunqi.permission.param.AclParam;
import com.xunqi.permission.pojo.SysAcl;
import com.xunqi.permission.pojo.SysRole;
import com.xunqi.permission.service.SysAclService;
import com.xunqi.permission.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/acl")
@Slf4j
@Api(tags = "权限点控制器")
public class SysAclController {

    @Resource
    private SysAclService sysAclService;
    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation(value = "权限点新增功能",httpMethod = "POST",protocols = "HTTP",produces = "/save.json",response = SysAcl.class)
    @RequestMapping(value = "/save.json")
    @ResponseBody
    public JsonData saveAclModule(AclParam param) {
        sysAclService.save(param);
        return JsonData.success();
    }

    @ApiOperation(value = "权限点修改功能",httpMethod = "POST",protocols = "HTTP",produces = "/update.json",response = SysAcl.class)
    @RequestMapping(value = "/update.json")
    @ResponseBody
    public JsonData updateAclModule(AclParam param) {
        sysAclService.update(param);
        return JsonData.success();
    }

    @ApiOperation(value = "权限点分页展示",httpMethod = "GET",protocols = "HTTP",produces = "/page.json",response = SysAcl.class)
    @RequestMapping(value = "/page.json")
    @ResponseBody
    public JsonData list(@RequestParam("aclModuleId") Integer aclModuleId, PageQuery pageQuery) {
        return JsonData.success(sysAclService.getPageByAclModuleId(aclModuleId,pageQuery));
    }

    @ApiOperation(value = "查看该角色被分配的权限",httpMethod = "GET",protocols = "HTTP",produces = "/acls.json",response = JsonData.class)
    @RequestMapping(value = "/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("aclId") int aclId) {
        Map<String,Object> map = Maps.newHashMap();
        List<SysRole> roleList = sysRoleService.getRoleListByAclId(aclId);
        map.put("roles",roleList);
        map.put("users",sysRoleService.getUserListByRoleList(roleList));
        return JsonData.success(map);
    }

}
